package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.Utils.ZipUtils;
import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.local.LocalData;
import com.jzg.jcptadmin.data.vo.SuccessData;
import com.jzg.jcptadmin.viewinterface.ToUpLoadTaskInterface;
import com.squareup.okhttp.RequestBody;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/3/4.
 */
public class ToUpLoadTaskPresenter extends BasePresenter<ToUpLoadTaskInterface> {
    private DataManager dataManager;
    private ToUpLoadTaskInterface mView;
    private Subscription mSubscription;

    public ToUpLoadTaskPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void attachView(ToUpLoadTaskInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null) mSubscription.unsubscribe();
    }

    public void upLoadTaskData(String path, final int position, int type) {
        if (!AppContext.isNetWork)
            return;
        Map<String, RequestBody> params;
        if (type == 0) {
            params = mView.getLoadTaskParams();
        } else {
            params = mView.getLoadTaskParams1();
        }


        mSubscription = dataManager.upLoadTaskData(params, path, type).
                observeOn(AndroidSchedulers.mainThread()).
                subscribeOn(Schedulers.io()).subscribe(new Observer<SuccessData>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null&& mView!=null) {
                    mView.showError(e.getMessage());
                }
            }

            @Override
            public void onNext(SuccessData data) {
                mView.showResult(data, position);
            }
        });
    }



    public String fiterZH(String path) {
        List<String> list = new ArrayList<>(Arrays.asList(LocalData.args));//文件名
        File file = new File(path);
        ArrayList<String> names = new ArrayList<>();//文件名
        if (file.exists()) {
            boolean hasVideo = true;
            File[] files = file.listFiles();//先获取目录下所有的照片
            for (File f : files) {
                String fname = f.getName();
                if(fname.endsWith("jpg")){
                    fname =  fname.substring(0,fname.length()-4);
                }
                if(fname.endsWith("mp4")){
                    hasVideo =false;
                }
                names.add(fname);
            }
            if (hasVideo){
                return "该车辆没有视频信息!";
            }
        }
        list.removeAll(names);//
        if (list.size() > 0) {
            StringBuffer sb = new StringBuffer("缺少");
            for (String string : list) {
                sb.append(LocalData.ZHpicMap.get(string)+"  ");
            }
            return sb.toString();
        } else {
            return "0";
        }
    }
    public String fiterKS(String path) {
        List<String> list = new ArrayList<>(Arrays.asList(LocalData.args1));//文件名
        File file = new File(path);
        List<String> names = new ArrayList<>();//文件名
        if (file.exists()) {
            File[] files = file.listFiles();//先获取目录下所有的照片
            for (File f : files) {
                String fname = f.getName();
                if(fname.endsWith("jpg")){
                    fname =  fname.substring(0,fname.length()-4);
                }
                names.add(fname);
            }
        }
        list.removeAll(names);//
        if (list.size() > 0) {
            StringBuffer sb = new StringBuffer("缺少");
            for (String string : list) {
                sb.append(LocalData.KSpicMap.get(string)+"  ");
            }
            return sb.toString();
        } else {
            return "0";
        }
    }

    public void getZip(final String path, final String path1, final int position, final int type) {
        Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                try {
                    String msg;
                    if (type == 0) {
                        msg = fiterZH(path);
                    }else{
                        msg = fiterKS(path);
                    }
                    if(msg.equals("0")){
                        ZipUtils.zipFiles(path, path1);
                        //联网上传
                        subscriber.onNext(position);
                    }else{
                        Throwable th = new Throwable(msg);
                        subscriber.onError(th);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }


            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Integer>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.toString());
            }

            @Override
            public void onNext(Integer position) {
                upLoadTaskData(path1, position, type);
            }
        });
    }
}
